package com.itheima.Jiuye16;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class ReflectDemo01 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        //mthed1();
        //mtherd2();
        //mtherd3();
    }

    private static void mtherd3() throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        Class<?> aClass = Class.forName("com.itheima.Jiuye16.Student"); //获取一个Class对象
        //获取有参构造，注意：参数为想要获取类对应的有参构造数据类型的.class文件
        Constructor<?> constructor = aClass.getDeclaredConstructor(String.class,int.class);
        //利用newInstance方法创建Student对象
        Student student = (Student) constructor.newInstance("张三", 23);
        System.out.println(student);
    }

    private static void mtherd2() throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        //获取一个Class对象
        Class<?> aClass = Class.forName("com.itheima.Jiuye16.Student");
        //获取构造方法对象
        Constructor constructor = aClass.getDeclaredConstructor();
        //用反射强行使用私有化的构造方法，需要临时取消访问检查
        constructor.setAccessible(true);
        //直接创建student对象
        Student student = (Student) constructor.newInstance();
        System.out.println(student);
    }

    private static void mthed1() throws ClassNotFoundException {
        Class aClass = Class.forName("com.itheima.Jiuye16.Student");
        Constructor[] constructors = aClass.getConstructors();
        System.out.println(constructors);
    }
}
